package view.hero
{
import core.Language;
import core.StageProxy;
import core.model.PlayerModel;

import flash.display.MovieClip;
import flash.events.Event;

import framework.proxy.ComponentProxy;
import framework.view.components.bar.ProgressBarProxy;
import framework.view.components.button.ButtonProxy;

import mx.utils.StringUtil;

import values.avatar.IAvatarValue;
import values.avatar.IPropertyProvider;
import values.events.AvatarValueEvent;

import view.component.dialog.ConfirmDialog;

/**
 * HeroDetailUIProxy
 * @author jliang
 *
 */
public class HeroDetailUIProxy extends ComponentProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function HeroDetailUIProxy()
	{
		super();
	}
	
	[Inject]
	public var playerModel:PlayerModel;
	
	//==========================================================================
	//  Properties
	//==========================================================================
	
	public function get xpBar():ProgressBarProxy
	{
		return proxys["xpBar"];
	}
	
	public function get property():HeroPropertyInfoProxy
	{
		return proxys["property"];
	}
	
	public function get avatar():HeroAvatarDetailProxy
	{
		return proxys["avatar"];
	}
	
	public function get skill():HeroSkillInfoProxy
	{
		return proxys["skill"];
	}
	
	public function get detail():HeroPropertyDetailProxy
	{
		return proxys["detailMc"];
	}
	
	private function get strongerBtn():ButtonProxy
	{
		return proxys["strongerBtn"];
	}
	
	private function get abandonBtn():ButtonProxy
	{
		return proxys["abandonBtn"];
	}
	
	private function get trainningBtn():ButtonProxy
	{
		return proxys["trainningBtn"];
	}
	
	private function get formationBtn():ButtonProxy
	{
		return proxys["formationBtn"];
	}
	
	override public function dispose():void
	{
		vo = null;
		super.dispose();
	}
	
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
		detail.visible = false;
		property.addEventListener(Event.SELECT , onBaseSelected);
	}
	
	private function onBaseSelected(evt:Event):void
	{
		detail.visible = true;
	}
	
	protected var vo:IAvatarValue;
	
	public function set data(value:*):void
	{
		if(vo)
		{
			vo.removeEventListener(AvatarValueEvent.PROPERTY_CHANGE, propertyChangedHandler);
			vo.removeEventListener(AvatarValueEvent.REMOVE_EQUIPMENT , updateEquipment);
			vo.removeEventListener(AvatarValueEvent.ADD_EQUIPMENT , updateEquipment);
		}
		vo = value;
		
		
		xpBar.minValue = vo.levelValue.prevResource;
		xpBar.maxValue = vo.levelValue.nextResource;
		xpBar.currentValue = vo.xp;
        
				
		property.data = vo;
		avatar.data = vo;
		skill.data = vo;
		detail.data = vo;
		
		vo.addEventListener(AvatarValueEvent.PROPERTY_CHANGE, propertyChangedHandler);
		
		if(vo == playerModel.myPlayerAvatar)
		{
			strongerBtn.enabled = false;
			abandonBtn.enabled = false;
		}
		else
		{
			strongerBtn.enabled = true;
			abandonBtn.enabled = true;
			vo.addEventListener(AvatarValueEvent.REMOVE_EQUIPMENT , updateEquipment);
			vo.addEventListener(AvatarValueEvent.ADD_EQUIPMENT , updateEquipment);
			updateEquipment();
		}
	}
	
	private function updateEquipment(evt:Event = null):void
	{
		if(vo.hasEquipment)
		{
			abandonBtn.enabled = false;
		}
		else
		{
			abandonBtn.enabled = true;
		}
	}
	
	public function get data():*
	{
		return vo;
	}
	
	public function get avatarProperty():IPropertyProvider
	{
		return data;
	}
	//==========================================================================
	//  Overridden methods: RepeaterRenderProxy
	//==========================================================================	
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:"xpBar", classRef: ProgressBarProxy, id:"xpBar" },
			{ skin:"property", classRef: HeroPropertyInfoProxy, id:"property" },
			{ skin:"avatar", classRef: HeroAvatarDetailProxy, id:"avatar" },
			{ skin:"skill", classRef: HeroSkillInfoProxy, id:"skill" },
			{ skin:"strongerBtn", classRef:ButtonProxy , id:"strongerBtn" , onClick:onStrongerBtnClick , text:Language.getText("@stronger_btn_desc") },
			{ skin:"abandonBtn" , classRef:ButtonProxy , id:"abandonBtn" , onClick:onAbandonBtnClick , text:Language.getText("@abandon_btn_desc") },
			{ skin:"trainningBtn" , classRef:ButtonProxy , id:"trainningBtn" , onClick:onTrainningBtnClick , text:Language.getText("@trainning_btn_desc") },
			{ skin:"formationBtn" , classRef:ButtonProxy , id:"formationBtn" , onClick:onFormationBtnClick , text:Language.getText("@formation_btn_desc") },
			{ skin:"detailMc" , classRef:HeroPropertyDetailProxy , id:"detailMc"}
		];
	}
	
	private function onTrainningBtnClick():void
	{
		
	}
	
	private function onFormationBtnClick():void
	{
		
	}
	
	private function onStrongerBtnClick():void
	{
		
	}
	
	private function onAbandonBtnClick():void
	{
		ConfirmDialog.show(StringUtil.substitute(Language.getText("@abandon_avatar_confirm_desc") , vo.template.name) , onConfirmClick);
	}
	
	private function onConfirmClick():void
	{
        StageProxy.lock();
		playerModel.abandonHero(vo);
	}
	
	protected function propertyChangedHandler(event:AvatarValueEvent):void
	{
		property.update();
		detail.update();
	}
}
}




